﻿using UnityEngine;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;
using GameFramework.common;
using GameFramework.net.nettest;
using UnityEngine.UI;

namespace GameFramework.net
{
    public class NetManager:Singleton<NetManager>
    {
        private List<Client> _sockets = new List<Client>();

        private Stack<int> clientIndex = new Stack<int>();
        public bool isClient = false;
        public NetManager()
        {
            clientIndex.Push(1);
            clientIndex.Push(2);
        }

        public void broadCastMsg(string msg)
        {
            foreach (Client client in _sockets)
            {
                client.send(msg);

            }
        }

        public int getClientCount()
        {
            return _sockets.Count;
        }

        public void addClient(Client newClient)
        {
            if (clientIndex.Count == 0)//满了
            {
                newClient.close();
                return;
            }

            Client client = _sockets.Find(c=>c.name.Equals(newClient.name));
            if (client != null)
            {
                
                _sockets.Remove(client);
                Dictionary<string, object> result = new Dictionary<string, object>();
                result.Add("msg","名字被挤了");
                client.send(JsonUtility.ToJson(result));
                client.close();
            }
            
            _sockets.Add(newClient);
            newClient.start();
            ClientChangeEvent changeEvent = new ClientChangeEvent();
            changeEvent.add = true;
            changeEvent.changeClient = newClient;
            changeEvent.nowClientNum = _sockets.Count;
            EventManager.Instance.dispatch(changeEvent);
        }
        
        public void removeClient(string name)
        {
            Client client = _sockets.Find(c=>c.name.Equals(name));
            if (client != null)
            {
                _sockets.Remove(client);
                client.close();
            }
        }

        public void closeAllClient()
        {
            foreach (Client client in _sockets)
            {
                client.close();
            }
            _sockets.Clear();
        }

        public Client createClient(Socket socket, string clientName)
        {
            if (clientIndex.Count == 0)
            {
                //客户端数量满了
                socket.Close();
                return null;
            }

            Client client = new Client(socket,clientName);
            int pop = clientIndex.Pop();
            client.index = pop;
            client.basePos = new Vector3(3, -2.5f * pop);
            GameObject[] objects = GameObject.FindGameObjectsWithTag("client");
            foreach (GameObject gameObject in objects)
            {
                if (!gameObject.activeSelf)
                {
                    client.obj = gameObject;
                    gameObject.SetActive(true);
                    Text text = gameObject.transform.GetComponent<Text>();
                    text.text = client.name;
                    break;
                }
            }

            return client;
        }


    }
}